package com.ibiaoke.system.service;

import com.ibiaoke.system.dao.AreaCityDao;
import com.ibiaoke.system.dao.AreaDistrictDao;
import com.ibiaoke.system.dao.AreaProvinceDao;
import com.ibiaoke.system.entity.AreaCity;
import com.ibiaoke.system.entity.AreaDistrict;
import com.ibiaoke.system.entity.AreaProvince;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * Created by biaoke on 2017/6/25.
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaProvinceDao areaProvinceDao;
    @Autowired
    private AreaCityDao areaCityDao;
    @Autowired
    private AreaDistrictDao areaDistrictDao;

    private static List<AreaProvince> areaProvinces;
    private static List<AreaCity> areaCities;
    private static List<AreaDistrict> areaDistricts;

    /**
     * 获取省市区
     *
     * @return
     */
    @Override
    public List<AreaProvince> listProvince() {
        if (areaProvinces == null) {
            //获取省市区
            areaProvinces = areaProvinceDao.listProvince();
            List<AreaDistrict> districts = listDistrict();
            List<AreaCity> cities = listCity();
            //把区县放入市
            Map<Integer, List<AreaDistrict>> districtMap = districts.stream().collect(groupingBy(AreaDistrict::getCityId));
            cities.forEach(city -> city.setDistricts(districtMap.get(city.getCityId())));
            //把市放入省
            Map<Integer, List<AreaCity>> cityMap = cities.stream().collect(groupingBy(AreaCity::getProvId));
            areaProvinces.forEach(province -> province.setCities(cityMap.get(province.getProvId())));

        }
        return areaProvinces;
    }

    /**
     * 获取所有城市
     *
     * @return
     */
    private List<AreaCity> listCity() {
        if (areaCities == null) {
            areaCities = areaCityDao.listCity();
        }
        return areaCities;
    }

    /**
     * 获取所有区县
     *
     * @return
     */
    private List<AreaDistrict> listDistrict() {
        if (areaDistricts == null) {
            areaDistricts = areaDistrictDao.listDistrict();
        }
        return areaDistricts;
    }

    /**
     * 获取指定省份下所有城市
     *
     * @return
     */
    @Override
    public List<AreaCity> listCity(Integer provinceId) {
        return listCity().stream().filter(areaCity -> Objects.equals(areaCity.getProvId(), provinceId))
                .collect(Collectors.toList());
    }

    /**
     * 获取指定城市下所有区县
     *
     * @return
     */
    @Override
    public List<AreaDistrict> listDistrict(Integer cityId) {
        return listDistrict().stream().filter(areaDistrict -> Objects.equals(areaDistrict.getCityId(), cityId))
                .collect(Collectors.toList());
    }

    /**
     * 获取省份名称
     *
     * @return
     */
    @Override
    public String getProvinceName(Integer provinceId){
        Optional<AreaProvince> optional = listProvince().stream().filter(areaProvince -> Objects.equals(provinceId, areaProvince.getProvId())).findAny();
        if(optional.isPresent()){
            return optional.get().getProvName();
        }
        return null;
    }

    /**
     * 获取城市名称
     *
     * @return
     */
    @Override
    public String getCityName(Integer cityId){
        Optional<AreaCity> optional = listCity().stream().filter(areaCity -> Objects.equals(cityId, areaCity.getCityId())).findAny();
        if(optional.isPresent()){
            return optional.get().getCityName();
        }
        return null;
    }

    /**
     * 获取区县名称
     *
     * @return
     */
    @Override
    public String geDistrictName(Integer districtId){
        Optional<AreaDistrict> optional = listDistrict().stream().filter(areaDistrict -> Objects.equals(districtId, areaDistrict.getDistrictId())).findAny();
        if(optional.isPresent()){
            return optional.get().getDistrictName();
        }
        return null;
    }

}
